package com.menggudf.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.menggudf.dao.AdminPermissionDao;
import com.menggudf.dao.PermissionDao;
import com.menggudf.dao.SystemAdminDao;
import com.menggudf.entity.AdminPermissions;
import com.menggudf.entity.Permission;
import com.menggudf.entity.SystemAdmin;
import com.menggudf.model.ResultModel;
import com.menggudf.service.AdminPermissionServiceIf;

@Service
public class AdminPermissionService implements AdminPermissionServiceIf{

    private static final Logger LOGGER = LoggerFactory.getLogger(AdminPermissionService.class);
    
    @Autowired
    AdminPermissionDao adminPermissionDao;
    
    @Autowired
    PermissionDao permissionDao;
    
    @Autowired
    SystemAdminDao systemAdminDao;
    
    @Override
    @Transactional(value="medicalTM", rollbackFor = Exception.class)
    public ResultModel setAdminPerssion(String adminCode, List<Integer> permissionIds) {
        
        AdminPermissions ap = new AdminPermissions();
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    @Transactional(value="medicalTM", readOnly=true)
    public ResultModel getAdminPerssion(String adminCode) {
        List<AdminPermissions> list = adminPermissionDao.getAdminPerssionByCode(adminCode);
        List<Integer> l = new ArrayList<Integer>();
        for(AdminPermissions a : list){
            l.add(a.getPermissionId());
        }
        return ResultModel.success("获取成功", l);
    }

    @Override
    @Transactional(value="medicalTM", rollbackFor = Exception.class)
    public ResultModel addAdminPerssion(String adminCode, String permissionid) {
        String[] sym = null ;
        if(permissionid.indexOf("\\s+|[，,]")==-1){
            sym = permissionid.split("\\s+|[，,]");
        }else{
            sym[0] = permissionid;
        }
        try {
            for(String id : sym){
                AdminPermissions a = new AdminPermissions();
                a.setAdminCode(adminCode);
                a.setCreatedTime(new Date());
                a.setPermissionId(Integer.valueOf(id));
                a.setPermissionCode(getPermissionCode(Integer.valueOf(id)));
                adminPermissionDao.persist(a);
            }
            return ResultModel.success();
        } catch (Exception e) {
            LOGGER.error("addAdminPerssion fail",e);
        }
        return ResultModel.fail();
    }

    @Override
    @Transactional(value="medicalTM", rollbackFor = Exception.class)
    public ResultModel delAdminPerssion(String adminCode, String permissionid) {
        String[] sym = null ;
        if(permissionid.indexOf("\\s+|[，,]")==-1){
            sym = permissionid.split("\\s+|[，,]");
        }else{
            sym[0] = permissionid;
        }
        try {
            for(String id : sym){
                AdminPermissions a = adminPermissionDao.getAdminPerssionByCodeAndId(adminCode,Integer.valueOf(id));
                adminPermissionDao.remove(a);
            }
        } catch (Exception e) {
            LOGGER.error("delAdminPerssion fail",e);
        }
        return ResultModel.fail();
    }
    
    private String getPermissionCode(Integer twoModule){
        return permissionDao.getPermissionCode(twoModule);
    }

    @Override
    @Transactional(value="medicalTM", readOnly=true)
    public Map<String, Object> getAdminPerssionCode(String adminCode) {
        SystemAdmin systemAdmin = systemAdminDao.findByAdminCode(adminCode);
        Map<String, Object> map = new HashMap<String, Object>();
        if(systemAdmin!=null){
            if(systemAdmin.getFlag()==1){
                List<Permission> plist = permissionDao.getAllPerssions();
                for(Permission p : plist){
                    map.put(p.getCode(),true);
                }
            }else{
                List<AdminPermissions> list = adminPermissionDao.getAdminPerssionByCode(adminCode);
                for(AdminPermissions a : list){
                    map.put(a.getPermissionCode(),true);
                }
            }
            return map;
        }else{
            return map;
        }
    }

}
